.PHONY: build 3rd skynet clean server client1 client2

all: help

help:
	@echo "支持下面命令："
	@echo "  make build						# 编译项目 "
	@echo "  make server						# 启动服务端 "
	@echo "  make client1						# 启动客户端1 "
	@echo "  make client2						# 启动客户端2 "
	@echo "  make console						# 启动控制台 "
	@echo "  make start_openresty					# 启动openresty "
	@echo "  make stop_openresty					# 关闭openresty "
	@echo "  make clean						# 清理 "

LUA_CLIB_PATH ?= luaclib
LUA_INCLUDE_DIR ?= skynet/3rd/lua

build: install_openresty install_resty_http install_resty_openssl 3rd skynet start_openresty 


3rd: 
	git submodule update --init
	cd 3rd/lua-cjson && $(MAKE) install LUA_INCLUDE_DIR=../../$(LUA_INCLUDE_DIR) DESTDIR=../.. LUA_CMODULE_DIR=./$(LUA_CLIB_PATH) CC='$(CC) -std=gnu99'

skynet:
	git submodule update --init
	cd skynet && $(MAKE) linux 

server: reload_openresty
	@./skynet/skynet etc/config

client1:
	@./skynet/3rd/lua/lua client/client.lua "127.0.0.1" 8080 "127.0.0.1" 8888 "cauchy" "123"

client2:
	@./skynet/3rd/lua/lua client/client.lua "127.0.0.1" 8080 "127.0.0.1" 8888 "AQ" "456"

install_openresty:
	sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates lsb-release
	sudo wget -O - https://openresty.org/package/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/openresty.gpg
	sudo echo "deb [arch=$$(dpkg --print-architecture) signed-by=/usr/share/keyrings/openresty.gpg] http://openresty.org/package/ubuntu  $$(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list > /dev/null
	sudo apt-get update
	sudo apt-get -y install --no-install-recommends openresty

install_resty_http:
	sudo wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/master/lib/resty/http.lua -P /usr/local/openresty/lualib/resty/
	sudo wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/master/lib/resty/http_headers.lua -P /usr/local/openresty/lualib/resty/
	sudo wget https://raw.githubusercontent.com/ledgetech/lua-resty-http/master/lib/resty/http_connect.lua -P /usr/local/openresty/lualib/resty/

install_resty_openssl:
	git submodule update --init
	sudo mkdir -p /usr/local/lib/lua/5.4/resty/openssl
	cd 3rd/lua-resty-openssl && sudo $(MAKE) install LUA_VERSION=5.4

start_openresty:
	@sudo openresty -c $(shell pwd)/openresty/conf/nginx.conf

stop_openresty:
	@sudo openresty -s quit -c $(shell pwd)/openresty/conf/nginx.conf

reload_openresty:
	@sudo openresty -s reload -c $(shell pwd)/openresty/conf/nginx.conf

console:
	@telnet 127.0.0.1 4040

clean: stop_openresty
	@rm -f $(LUA_CLIB_PATH)/*.so